package controller

import (
	"SelectCourse/dao"
	"SelectCourse/types"
	"github.com/gin-gonic/gin"
	"log"
	"net/http"
	"strconv"
)
//添加成员的逻辑在LoginController
/**
 * @Autnor Roy
 * @TODO 根据id获得成员的方法
 * @Date 21:12 2022/2/7
 **/
func GetMember(c *gin.Context) {
	//绑定数据
	var getMember types.GetMemberRequest
	var response types.GetMemberResponse
	if err := c.ShouldBind(&getMember); err != nil {
		log.Println(err)
		response.Code = types.ParamInvalid
		c.JSON(http.StatusOK, response)
		return
	}
	//将string转换为int
	i, err := strconv.Atoi(getMember.UserID)
	if err!=nil{
		log.Println(err)
		response.Code = types.ParamInvalid
		c.JSON(http.StatusOK, response)
		return
	}
	//创建一个member以保存查询结果
	var member types.TMember
	member.ID = uint(i)
	dao.Db.First(&member)
	log.Println(member)

	//用户名长度为0为空则没查到
	if len(member.Username) == 0 {
		response.Code = types.UserNotExisted
		c.JSON(http.StatusOK, response)
		return
	} else {
		//否则判断是否已删除
		if member.IsDelete == 1 {
			response.Data = member
			response.Code = types.UserHasDeleted
			c.JSON(http.StatusOK, response)
			return
		} else {
			//未删除正常返回
			response.Data = member
  			response.Code = types.OK
			c.JSON(http.StatusOK, response)
			return
		}
	}
	response.Code = types.UnknownError
	//上面的逻辑都没捕获到，返回未知错误
	c.JSON(http.StatusOK, response)
	return
}

/**
 * @Autnor Roy
 * @TODO 批量获取member
 * @Date 15:01 2022/2/8
 **/
func GetMemberList(c *gin.Context) {
	//绑定数据
	var getMemberListRequest types.GetMemberListRequest
	var getMemberListResponse types.GetMemberListResponse
	if err := c.ShouldBind(&getMemberListRequest); err != nil {
		log.Println(err)
		getMemberListResponse.Code = types.ParamInvalid
		c.JSON(http.StatusOK, getMemberListResponse)
		return
	}
	//创建对象以返回查询结果
	var members []types.TMember
	dao.Db.Limit(getMemberListRequest.Limit).Offset(getMemberListRequest.Offset).Find(&members)
	getMemberListResponse.Data.MemberList = members
	c.JSON(http.StatusOK, getMemberListResponse)
	return
}

/**
 * @Autnor zkm
 * @TODO 修改成员的信息
 * @Date 13:41 2022/2/9
 **/
func UpdateMember(c *gin.Context) {
	/*
		逻辑： 先判断是否用户存在 => 返回不存在码
					||
					V
				判断是否需要写入 => 返回正确
					||
					V
				 完成修改
				 	||
					V
				返回正确
	*/
	// 绑定成员
	var getMember types.UpdateMemberRequest
	var response types.UpdateMemberResponse

	if err := c.ShouldBind(&getMember); err != nil {
		log.Println("bind error")
		response.Code = types.ParamInvalid
		c.JSON(http.StatusOK, response)
		return
	}

	//usertype 判断
	i, err := strconv.Atoi(getMember.UserID)
	if err != nil {
		log.Println(err)
		response.Code = types.ParamInvalid
		c.JSON(http.StatusOK, response)
		return
	}
	var nickname = getMember.Nickname
	if len(nickname) > 20 || len(nickname) == 0 {
		response.Code = types.ParamInvalid
		c.JSON(http.StatusOK, response)
		return
	}

	//创建一个member以保存查询结果
	var member types.TMember
	member.ID = uint(i)
	dao.Db.First(&member)
	log.Println(member)
	if len(member.Username) == 0 {
		response.Code = types.UserNotExisted
		c.JSON(http.StatusOK, response)
		return
	} else {
		//否则判断是否已删除
		if member.IsDelete == 1 {
			response.Code = types.UserHasDeleted
			c.JSON(http.StatusOK, response)
			return
		} else {
			//未删除正常返回
			response.Code = types.OK
			// 如果名字不一致去更新数据库
			if member.Nickname != nickname {
				member.Nickname = nickname
				dao.Db.Save(&member)
			}
			c.JSON(http.StatusOK, response)
			return
		}
	}
	response.Code = types.UnknownError
	c.JSON(http.StatusOK, response)
	return
}

// /**
//  * @Autnor zkm
//  * @TODO 在删除的时候增加删除的时间
//  * @Date 13:41 2022/2/9
//  **/
func DeleteMember(c *gin.Context) {
	// 调用update函数将用户的is_delete改成true
	var getMember types.DeleteMemberRequest
	var response types.DeleteMemberResponse
	if err := c.ShouldBind(&getMember); err != nil {
		log.Println(err)
		response.Code = types.ParamInvalid
		c.JSON(http.StatusOK, response)
		return
	}

	//usertype 判断
	i, err := strconv.Atoi(getMember.UserID)
	if err != nil {
		log.Println(err)
		response.Code = types.ParamInvalid
		c.JSON(http.StatusOK, response)
		return
	}

	var member types.TMember
	member.ID = uint(i)
	dao.Db.First(&member)
	log.Println(member)
	if len(member.Username) == 0 {
		response.Code = types.UserNotExisted
		c.JSON(http.StatusOK, response)
		return
	} else {
		//否则判断是否已删除
		if member.IsDelete == 1 {
			response.Code = types.UserHasDeleted
			c.JSON(http.StatusOK, response)
			return
		} else {
			//未删除正常返回
			response.Code = types.OK
			// 如果名字不一致去更新数据库
			member.IsDelete = 1
			dao.Db.Save(&member)
			c.JSON(http.StatusOK, response)
			return
		}
	}
	response.Code = types.UnknownError
	c.JSON(http.StatusOK, response)
	return
}
